﻿var subs, tmList = [];


//类别切换
$('#nav li').click(function () {
    var _this = $(this);
    if (_this.hasClass('cur')) {
        return;
    }

    $('#nav .cur').removeClass('cur');
    _this.addClass('cur');

    //清除所有定时器
    for (var i = 0; i < tmList.length; i++) {
        if (tmList[i]) {
            clearTimeout(tmList[i]);
        }
    }
    tmList = [];

    var exchCode = $(this).data('exchcode');
    var name = $(this).data('name');
    if (!exchCode) {
        //全部
        subs = list['天交所'];
        subs = subs.concat(list['广贵所']);
        subs = subs.concat(list['国际外汇']);
        subs = subs.concat(list['全球现货']);
        subs = subs.concat(list['全球指数']);

        buildHtml();

        GeRefreshData('TJPME');
        GeRefreshData('PMEC');
        GeRefreshData('WH');
        GeRefreshData('WGJS');
        GeRefreshData('GJZS');
        GeRefreshData('NYMEX');
    } else {
        subs = list[name];
        buildHtml();
        GeRefreshData(exchCode);

        if (name == '全球现货') {
            //原油
            GeRefreshData('NYMEX');
        }
    }

}).eq(0).click();

function buildHtml() {
    var item, html = '';
    for (var i = 0; i < subs.length; i++) {
        item = subs[i];
        html += '<a class="item_hq ' + item.SYMBOL_CODE + '" href="hq.html?exchCode=' + item.EXCH_CODE + '&symbolCode=' + item.SYMBOL_CODE + '&codeType=' + item.codeType + '&name=' + item.SYMBOL_NAME + '" id="' + item.SYMBOL_CODE + '" data-i="' + i + '">' +
                    '<p class="p1">' + item.SYMBOL_NAME + '</p>' +
                    '<p class="p2">...</p>' +
                    '<p class="p3">...</p>' +
                '</a>';
    }
    $('#panel').html(html);
}

function GeRefreshData(exchCode) {
    //定时刷新数据
    var tmp = setTimeout(function () {
        $.ajax({
            url: quotes + '?exchName=' + exchCode + '&st=' + Math.random(),
            dataType: 'jsonp',
            jsonp: "callback",
            success: function (data) {
                $('#panel .item_hq').each(function () {
                    var _this = $(this),
                        _id = _this[0].id,
                        p2 = _this.find('.p2'),
                        p3 = _this.find('.p3'),
                        item, dif;

                    for (var i = 0; i < data.i.length; i++) {
                        item = data.i[i];
                        if (item == _id) {
                            //先判断当前价是否变更再操作DOM，提升性能
                            if (data.c[i] != p2.html()) {
                                var PRICE_SCALE = getDecimal(subs[_this.data('i')].PRICE_SCALE);
                                dif = toFixed((data.c[i] - data.p[i]), PRICE_SCALE);

                                p2.html(toFixed(data.c[i], PRICE_SCALE));
                                p3.html(dif + ',' + changeRate(data.c[i], data.p[i]) + '%');
                                if (dif > 0) {
                                    p2.addClass('red').removeClass('green');
                                    p3.addClass('red').removeClass('green');
                                } else if (dif < 0) {
                                    p2.addClass('green').removeClass('red');
                                    p3.addClass('green').removeClass('red');
                                }
                            }
                            break;
                        } else {
                            continue;
                        }
                    }
                });

                GeRefreshData(exchCode);
            }
        });
    }, sxtime);
    tmList.push(tmp);
}

